package vip.xiaonuo.smzq.modular.kcl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.beanutils.BeanUtils;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb06Kc;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb13Zycllx;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb06KcMapper;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb13ZycllxMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.Bazycl;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.BazyclDetails;
import vip.xiaonuo.smzq.modular.kcl.mapper.BazyclMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IBazyclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 备案资源储量 服务实现类
 *
 * @author eomer
 * @since 2024-02-22
 */
@Service
public class BazyclServiceImpl extends ServiceImpl<BazyclMapper, Bazycl> implements IBazyclService {

    @Resource
    private BazyclMapper bazyclMapper;

    @Resource
    private Cb06KcMapper cb06KcMapper; //矿产

    @Resource
    private Cb13ZycllxMapper cb13ZycllxMapper;//资源储量类型


    @Override
    public Page<Bazycl> page(Bazycl bazycl) {
        QueryWrapper<Bazycl> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     *
     * 备案资源储量查询list
     *
     * @param kqbm 矿区编号
     * @param djflbm 登记分类编码
     * @param nd 年度
     * @return
     */
    @Override
    public List<List<BazyclDetails>> getBazyclList(String kqbm, Integer djflbm, Integer nd) {
        List<List<BazyclDetails>> resultList = new ArrayList<>();
        List<Integer> bgsxhList = bazyclMapper.getBgsxhList(kqbm, djflbm, nd);
         if (bgsxhList!=null &&bgsxhList.size() > 0){
             for (Integer bgsxh : bgsxhList) {
                 QueryWrapper<Bazycl> queryWrapper = new QueryWrapper<>();
                 queryWrapper.eq("bgsxh",bgsxh);
                 queryWrapper.eq("nd",nd);
                 queryWrapper.eq("kqbh",kqbm);
                 queryWrapper.eq("djflbh",djflbm);
                 List<Bazycl> bazyclList = bazyclMapper.selectList(queryWrapper);
                 if (bazyclList!=null &&bazyclList.size() > 0){
                     List<BazyclDetails> bazyclDetailsList = new ArrayList<>();
                     for (Bazycl bazycl : bazyclList) {
                         BazyclDetails bazyclDetails=new BazyclDetails();
                         try {
                             BeanUtils.copyProperties(bazyclDetails,bazycl);

                             //统计对象及资源储量单位
                             QueryWrapper<Cb06Kc> cb06Kcquery = new QueryWrapper<>();
                             cb06Kcquery.eq("kcdm", bazyclDetails.getKcdm());
                             cb06Kcquery.eq("tjdx",bazyclDetails.getTjdx());
                             Cb06Kc cb06Kc = cb06KcMapper.selectOne(cb06Kcquery);
                             bazyclDetails.setKcmc(cb06Kc.getKcmc());
                             //判断是否是种类型
                             if (bazyclDetails.getTjdx()>0){
                                 bazyclDetails.setCldw(cb06Kc.getJsdxmc()+" "+cb06Kc.getTbdw());
                             }else{
                                 bazyclDetails.setCldw(cb06Kc.getKsldw());
                             }

                             //类型编码
                             QueryWrapper<Cb13Zycllx> queryZycllx = new QueryWrapper<>();
                             queryZycllx.eq("dm", bazyclDetails.getZycllx());
                             Cb13Zycllx cb13Zycllx = cb13ZycllxMapper.selectOne(queryZycllx);
                             //类型编码
                             bazyclDetails.setZyclmc(cb13Zycllx.getMc());

                             bazyclDetailsList.add(bazyclDetails);

                         } catch (IllegalAccessException e) {
                             e.printStackTrace();
                         } catch (InvocationTargetException e) {
                             e.printStackTrace();
                         }
                     }
                     resultList.add(bazyclDetailsList);
                 }

             }



         }



        return resultList;
    }
    //添加备案资源储量
    @Override
    public int addList(List<List<BazyclDetails>> bazyclList, String kqbh, Integer djflbm, Integer nd) {
        QueryWrapper<Bazycl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nd",nd);
        queryWrapper.eq("kqbh",kqbh);
        queryWrapper.eq("djflbh",djflbm);
        bazyclMapper.delete(queryWrapper);
        int count = 0;
          for (int i=0;i<bazyclList.size();i++) {
              List<BazyclDetails> bazyclDetailsList=bazyclList.get(i);
            for (BazyclDetails bazyclDetails : bazyclDetailsList) {
                Bazycl bazycl = new Bazycl();
                    try {
                        BeanUtils.copyProperties(bazycl,bazyclDetails);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    bazycl.setKqbh(kqbh);
                    bazycl.setNd(nd);
                    bazycl.setDjflbh(djflbm);
                    bazycl.setBgsxh(i+1);
                count+= bazyclMapper.insert(bazycl);
            }
        }

        return count;
    }

    @Override
    public List<Bazycl> selectList(QueryWrapper<Bazycl> queryWrapper) {
        return bazyclMapper.selectList(queryWrapper);
    }


}
